import queue

class BasicEvent:
    ID = 0
    def __init__(self):
        pass

class TickEvent(BasicEvent):
    def __init__(self):
        super().__init__()

class KeyPressedEvent(BasicEvent):
    def __init__(self, key: int):
        super().__init__()

        self.key = key

class KeyReleasedEvent(BasicEvent):
    def __init__(self, key: int):
        super().__init__()

        self.key = key

class EventHandlerGroup:
    def __init__(self):
        self.group = []
    
    def add(self, func):
        self.group.append(func)
    
    def execute(self, event):
        for i in self.group:
            i(event)

class EventBus:

    event_queue = queue.Queue()
    handler = []

    @classmethod
    def post(cls, event: BasicEvent):
        cls.event_queue.put(event)
    
    @classmethod
    def execute(cls):
        if cls.event_queue.empty():
            return
        
        event = cls.event_queue.get()
        cls.handler[event.ID].execute(event)
    
    @classmethod
    def register(cls, event):
        event.ID = cls.handler.__len__()
        cls.handler.append(EventHandlerGroup())

    @classmethod
    def on(cls, event):
        def wrapper(f):
            
            cls.handler[event.ID].add(f)
        
        return wrapper
